using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using eShop.Business;
using ECommerce.Library;
using ECommerce.Library.Web;
using eShop.Utility;
using eShop.Business;

public partial class myaccount_change_password : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SessionManager.IsUserLoggedIn)
            {
                if (SessionManager.ReturnUrl != null)
                {
                    string returnUrl = SessionManager.ReturnUrl;
                    SessionManager.ReturnUrl = null;
                    Response.Redirect(returnUrl);
                }              

            }
            else
            {
                Response.Redirect(CustomSiteMap.Login);
            }
        }

    }


    protected void imbModify_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                string emailAddress = tbxEmailAddress.Text.Trim();
                string password = tbxCurrentPassword.Text.Trim();
                string newPassword = tbxNewPassword.Text.Trim();
                divMessage.Attributes.Remove("class");
                if (!RegExUtil.IsEmail(emailAddress) || !RegExUtil.IsPassword(password) || !RegExUtil.IsPassword(newPassword))
                {
                    litMessage.Text = "Provided email address and password do not match.";
                    divMessage.Attributes.Add("class", "error");

                }
                if (UserManager.IsLoginValid(emailAddress, password))
                {
                    eShop.Model.User customerUser = UserManager.GetUserByUserName(emailAddress);
                    if (UserManager.UpdatePassword(customerUser.Id, newPassword))
                    {
                        litMessage.Text = "Your password has been successfully changed.";
                        divMessage.Attributes.Add("class", "success");
                    }
                    else
                    {
                        litMessage.Text = "Modifying your account information failed. Please try again";
                        divMessage.Attributes.Add("class", "error");
                    }
                }
                else
                {
                    litMessage.Text = "Provided email address and password do not match.";
                    divMessage.Attributes.Add("class", "error");
                }


            }
        }
        catch (Exception ex)
        {
            litMessage.Text = ex.Message;
            divMessage.Attributes.Remove("class");
            divMessage.Attributes.Add("class", "error");
        }
    }
}
